package com.gym.code.config.admin.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.gym.code.config.admin.bean.db.DBBaseMappingInfo;
import com.gym.code.config.admin.bean.table.TableBaseMappingBean;
import com.gym.code.config.admin.bean.table.TableMappingBean;
import com.gym.code.config.admin.client.DBHelpService;
import com.gym.code.config.admin.client.model.DBDatabaseModel;
import com.gym.code.config.admin.client.model.DBTableModel;
import com.gym.code.config.admin.service.IDBMappingService;
import com.gym.code.config.admin.service.ITableMappingService;
import com.gym.code.generator.utils.LocalStringUtils;

/**
 * @author xinchun.wang
 * @email: 532002108@qq.com
 */
@Controller
@RequestMapping("/table")
public class TableController {
	private final Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private IDBMappingService dbService;
	
	@Autowired
	private ITableMappingService tableMappingService;
	
	@Autowired
	private DBHelpService dbHelpService;

	@RequestMapping("/config")
	public ModelAndView config(Long dbId, String tableName) {
		logger.info("enter table config dbId {} tableName{}", dbId, tableName);
		List<TableMappingBean> tableList = tableMappingService.queryAll(dbId);
		TableMappingBean bean = null;
		for (TableMappingBean item : tableList) {
			if (StringUtils.equalsIgnoreCase(item.getTableName(), tableName)) {
				bean = item;
				break;
			}
		}
		if (bean == null) {
			Map<String, Object> model = new HashMap<String, Object>();
			model.put("tableName", tableName);
			String beanName = LocalStringUtils.toBeanName(tableName, true);
			model.put("beanName", beanName + "Bean");
			model.put("daoName", beanName + "Dao");
			model.put("serviceName", beanName + "Service");

			model.put("dbId", dbId);
			return new ModelAndView("table/add", model);
		} else {
			return new ModelAndView("redirect:/table/view.do?tableId=" + bean.getId() + "&tableName=" + bean.getTableName());
		}
	}

	@RequestMapping("/add")
	public ModelAndView view(Long dbId, String tableName, String beanName, String daoName, String serviceName) {

		TableBaseMappingBean bean = new TableBaseMappingBean();
		bean.setBeanName(beanName);
		bean.setTableName(tableName);
		bean.setServiceName(serviceName);
		bean.setDbId(dbId);
		bean.setDaoName(daoName);
		tableMappingService.insert(bean);
		Map<String,Object> model = new HashMap<String,Object>();
		model.put("dbId", dbId);
		model.put("tableName", tableName);
		return new ModelAndView("redirect:/table/view.do?tableId=" + bean.getId() + "&tableName=" + tableName);
	}
	
	@RequestMapping("/view")
	public ModelAndView view(Long tableId, String tableName) {
		TableMappingBean bean = tableMappingService.query(tableId);
		DBBaseMappingInfo baseDbBean = dbService.queryBase(bean.getDbId());
		DBDatabaseModel dbModel = DBController.wrapperDBTableModel(baseDbBean);
		Map<String,DBTableModel> dbTableMap  = dbHelpService.getTableInfoMap(dbModel);
		DBTableModel dbTableModel = dbTableMap.get(tableName);
		dbTableModel.setTableMappingBean(bean);
		Map<String,Object> model = new HashMap<String,Object>();
		model.put("bean", bean);
		model.put("tableModel", dbTableModel);
		return new ModelAndView("table/view",model);
	}

}
